package com.gratuity.widget;

import android.app.AlertDialog;
import android.app.PendingIntent;
import android.app.Service;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import android.widget.RemoteViews;
import android.widget.Toast;

public class GratuityWidgetProvider extends AppWidgetProvider {

	public static String AMOUNT_WIDGET_RECEIVER = "AmountReceiverWidget";
	public static String TIP_WIDGET_RECEIVER = "TipReceiverWidget";	
	public static String SPLIT_WIDGET_RECEIVER = "SplitReceiverWidget";
	public static int i = 0;
	
	@Override
	public void onEnabled(Context context) {
		// TODO Auto-generated method stub
		super.onEnabled(context);
		Log.e("Speedway","Widget onEnabled");
	}
	
	@Override
	public void onDeleted(Context context, int[] appWidgetIds) {
		// TODO Auto-generated method stub
		super.onDeleted(context, appWidgetIds);
		Log.e("Speedway","Widget onDeleted");
	}
	
	@Override
	public void onDisabled(Context context) {
		// TODO Auto-generated method stub
		super.onDisabled(context);
		Log.e("Speedway","Widget onDisabled");
	}
	
	@Override
	public void onReceive(Context context, Intent intent) {
		// TODO Auto-generated method stub
		super.onReceive(context, intent);
		Log.e("SampleWidget","Widget onReceive");
		//Amount Action
		if (intent.getAction().equals(AMOUNT_WIDGET_RECEIVER)) {
			String msg = "null";
			try {
			msg = intent.getStringExtra("msg");
			} catch (NullPointerException e) {
			Log.e("Error", "msg = null");
			}
			Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
		}
		
		//Split Action
		if (intent.getAction().equals(SPLIT_WIDGET_RECEIVER)) {
			String msg = "null";
			try {
			msg = intent.getStringExtra("msg");
			} catch (NullPointerException e) {
			Log.e("Error", "msg = null");
			}
			Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
		}		
	}
	
	@Override
	public void onUpdate(Context context, AppWidgetManager appWidgetManager,int[] appWidgetIds) {
		// TODO Auto-generated method stub
		super.onUpdate(context, appWidgetManager, appWidgetIds);
		Log.e("SampleWidget","Widget onUpdate");

		RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget);
		//Set Amount OnClickPendingIntent
		Intent amount = new Intent(context, GratuityWidgetProvider.class);
		amount.setAction(AMOUNT_WIDGET_RECEIVER);
		amount.putExtra("msg", "Message for Amount Click");
		PendingIntent amountPendingIntent = PendingIntent.getBroadcast(context, 0, amount, 0);
		remoteViews.setOnClickPendingIntent(R.id.etv_amount, amountPendingIntent);
		//Set Tip OnClickPendingIntent
		Intent tip = new Intent(context, MainActivity.class);
		tip.setAction(TIP_WIDGET_RECEIVER);
		tip.putExtra("msg", "Message for Tip Click");
		PendingIntent tipPendingIntent = PendingIntent.getBroadcast(context, 0, tip, 0);
		remoteViews.setOnClickPendingIntent(R.id.etv_tip, tipPendingIntent);
		//Set Split OnClickPendingIntent
		Intent split = new Intent(context, GratuityWidgetProvider.class);
		split.setAction(SPLIT_WIDGET_RECEIVER);
		split.putExtra("msg", "Message for Split Click");
		PendingIntent splitPendingIntent = PendingIntent.getBroadcast(context, 0, split, 0);
		remoteViews.setOnClickPendingIntent(R.id.etv_split, splitPendingIntent);
		appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);   
		
	}	
	
}
